%%%-------------------------------------------------------------------
%%% @author mirahs(2892727493@qq.com)
%%% @doc
%%%     网络通讯根监控树
%%% @end
%%%-------------------------------------------------------------------
-module(net_sup2).

-behaviour(supervisor).

-export([
    start_link/0
    ,stop/0

    ,init/1
]).

-include("common.hrl").
-include("env.hrl").


start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

stop() ->
    ?INFO("正在关闭[~w]", [?MODULE]),
    supervisor:terminate_child(?MODULE, net_tcp_accepter_sup),
    supervisor:terminate_child(?MODULE, net_tcp_listen_srv),
    supervisor:terminate_child(?MODULE, net_http_srv),
    ?INFO("已经关闭[~w]", [?MODULE]),
    ok.


init([]) ->
    SocketChildren = get_socket_children(env_mgr:get(?ENV_NET_TCP_PORT)),
    WebsocketChildren = get_http_children(env_mgr:get(?ENV_NET_HTTP_PORT)),
    {ok, {{one_for_one, 5, 10}, SocketChildren ++ WebsocketChildren}}.


get_socket_children(undefined) ->
    [];
get_socket_children(_Port) ->
    [
        {net_tcp_accepter_sup, {net_tcp_accepter_sup, start_link, []}, permanent, 10000, supervisor, [net_tcp_accepter_sup]}
        ,{net_tcp_listen_srv, {net_tcp_listen_srv, start_link, []}, permanent, brutal_kill, worker, [net_tcp_listen_srv]}
    ].

get_http_children(undefined) ->
    [];
get_http_children(_Port) ->
    [
        {net_http_srv, {net_http_srv, start_link, []}, permanent, brutal_kill, worker, [net_http_srv]}
    ].
